﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cninfo
{
    class Notice
    {
        private string ticker = "";

        public string Ticker
        {
            get { return ticker; }
            set { ticker = value; }
        }

        private string name = "";

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string title = "";

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        private string url = "";

        public string Url
        {
            get { return url; }
            set { url = value; }
        }

        private string size = "";

        public string Size
        {
            get { return size; }
            set { size = value; }
        }

        private string date = "";

        public string Date
        {
            get { return date; }
            set { date = value; }
        }

        private string msg = "";

        public string Msg
        {
            get { return msg; }
            set { msg = value; }
        }

        public bool ParseFromLine(string line, string domain)
        {
            string tag1 = "class=\"zz\">";
            string tag2 = "<a href=\"";
            string tag3 = "\" target=new>";
            string tag4 = "width=16> (";
            string tag5 = "class=\"time2\">";

            int start = 0;
            int p = 0;
            int q = 0;
            p = line.IndexOf(tag1, start);
            this.ticker = line.Substring(p + tag1.Length, 6);

            start = (p + tag1.Length + 6);
            p = line.IndexOf(tag2, start);
            q = line.IndexOf(tag3, start);
            this.url = domain + line.Substring(p + tag2.Length, q - (p + tag2.Length) );

            start = q + tag3.Length;
            p = q;
            q = line.IndexOf("</a>", start);
            string title = line.Substring(p + tag3.Length, q - (p + tag3.Length));
            this.name = title.Split(new string[] { "：" }, StringSplitOptions.None)[0];
            this.title = title.Split(new string[] { "：" }, StringSplitOptions.None)[1];

            start = q + 4;
            p = line.IndexOf(tag4, start);
            q = line.IndexOf(")</font>", start);
            this.size = line.Substring(p + tag4.Length, q - (p + tag4.Length) );

            start = q + 6;
            p = line.IndexOf(tag5, start);
            start = p;
            q = line.IndexOf("</span>", start);
            this.date = line.Substring(p + tag5.Length, q - (p + tag5.Length));

            //this.msg = "";

            return true;
        }

        public override bool Equals(object right)
        {
            Notice n = right as Notice;
            return (0 == string.Compare(this.Ticker, n.Ticker) &&
                0 == string.Compare(this.Name, n.Name) &&
                0 == string.Compare(this.Title, n.Title) &&
                0 == string.Compare(this.Url, n.Url) &&
                0 == string.Compare(this.Size, n.Size) &&
                0 == string.Compare(this.Date, n.Date));
        }

        public void print()
        {
            Console.WriteLine( string.Format("{0}, {1}:{2}({3}, {4}, {5})",
                this.Ticker, this.Name, this.Title, this.Size, this.Date, this.Url));
        }
    }
}

